{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c57413f8-1493-4246-bd76-ecd4641b0249",
   "metadata": {},
   "source": [
    "# Dynamic parameters\n",
    "\n",
    "To facilitate experimenting around weighting, placeholder `Param`s can be utilised at query definition, which can be filled when running the query. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1bf67328-efe5-4c88-9c36-ce2d4b20d89f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install superlinked==37.5.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "11664035-fff3-4c38-97f3-f2fbb0d46778",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from superlinked import framework as sl\n",
    "\n",
    "pd.set_option(\"display.max_colwidth\", 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2df24eaf-f9b8-404b-8b7f-0a9d2c2284df",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Paragraph(sl.Schema):\n",
    "    id: sl.IdField\n",
    "    body: sl.String\n",
    "    like_count: sl.Integer\n",
    "\n",
    "\n",
    "paragraph = Paragraph()\n",
    "\n",
    "body_space = sl.TextSimilaritySpace(text=paragraph.body, model=\"sentence-transformers/all-mpnet-base-v2\")\n",
    "like_space = sl.NumberSpace(number=paragraph.like_count, min_value=0, max_value=100, mode=sl.Mode.MAXIMUM)\n",
    "paragraph_index = sl.Index([body_space, like_space])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4aed826a-470e-4e53-8f2a-e8e529373b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "source: sl.InMemorySource = sl.InMemorySource(paragraph)\n",
    "executor = sl.InMemoryExecutor(sources=[source], indices=[paragraph_index])\n",
    "app = executor.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "61d76e31-72f4-4404-8cc1-31c08d08d978",
   "metadata": {},
   "outputs": [],
   "source": [
    "source.put(\n",
    "    [\n",
    "        {\n",
    "            \"id\": \"paragraph-1\",\n",
    "            \"body\": \"Glorious animals live in the wilderness.\",\n",
    "            \"like_count\": 75,\n",
    "        },\n",
    "        {\n",
    "            \"id\": \"paragraph-2\",\n",
    "            \"body\": \"Growing computation power enables advancements in AI.\",\n",
    "            \"like_count\": 10,\n",
    "        },\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c91428bb-de01-4c7e-9a4e-004188b95e2b",
   "metadata": {},
   "source": [
    "## Using dynamic Params in the query\n",
    "\n",
    "When defining the query, placeholder `Param`s can be used to indicate actual weights and other parameters will be filled when running the query."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "198f0bce-5d7c-412b-a989-d0ab60982770",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = (\n",
    "    sl.Query(\n",
    "        paragraph_index,\n",
    "        weights={\n",
    "            body_space: sl.Param(\"body_space_weight\"),\n",
    "            like_space: sl.Param(\"like_space_weight\", default=0.5),  # Params' default value can be overriden.\n",
    "        },\n",
    "    )\n",
    "    .find(paragraph)\n",
    "    .similar(body_space, sl.Param(\"query_text\"))\n",
    "    .select_all()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39f866d2-83c2-4991-ac91-3dd1b8934d29",
   "metadata": {},
   "source": [
    "`Param`s generally have default values. The general guiding principle is that the Param will not affect the query results unless explicitly specified to do so. To list the most important examples: \n",
    "- space weights (defined in the `weights` argument of a `Query`) default to `0.0` (unless overriden using the `default` argument of `Param`)\n",
    "- `.similar` and `.with_vector` clause inputs default to being empty, effectively getting removed from the `Query`\n",
    "- `.similar` and `.with_vector` clause weight `Param`s default to `1.0` (but with an empty default input they take no effect unless specified)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aaa75750-3b1c-4d68-af9c-133a249f6b0b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>like_count</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Growing computation power enables advancements in AI.</td>\n",
       "      <td>10</td>\n",
       "      <td>paragraph-2</td>\n",
       "      <td>0.454417</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Glorious animals live in the wilderness.</td>\n",
       "      <td>75</td>\n",
       "      <td>paragraph-1</td>\n",
       "      <td>-0.012840</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                    body  like_count  \\\n",
       "0  Growing computation power enables advancements in AI.          10   \n",
       "1               Glorious animals live in the wilderness.          75   \n",
       "\n",
       "            id  similarity_score  rank  \n",
       "0  paragraph-2          0.454417     0  \n",
       "1  paragraph-1         -0.012840     1  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "body_based_result = app.query(\n",
    "    query,\n",
    "    query_text=\"How computation power changed the course of AI?\",\n",
    "    body_space_weight=1,\n",
    "    like_space_weight=0,\n",
    ")\n",
    "\n",
    "sl.PandasConverter.to_pandas(body_based_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bb175e84-fea6-44f2-a06f-d4797c17e74b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>like_count</th>\n",
       "      <th>id</th>\n",
       "      <th>similarity_score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Glorious animals live in the wilderness.</td>\n",
       "      <td>75</td>\n",
       "      <td>paragraph-1</td>\n",
       "      <td>0.461940</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Growing computation power enables advancements in AI.</td>\n",
       "      <td>10</td>\n",
       "      <td>paragraph-2</td>\n",
       "      <td>0.078217</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                    body  like_count  \\\n",
       "0               Glorious animals live in the wilderness.          75   \n",
       "1  Growing computation power enables advancements in AI.          10   \n",
       "\n",
       "            id  similarity_score  rank  \n",
       "0  paragraph-1          0.461940     0  \n",
       "1  paragraph-2          0.078217     1  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "like_based_result = app.query(\n",
    "    query,\n",
    "    query_text=\"How computation power changed the course of AI?\",\n",
    "    body_space_weight=0,\n",
    "    like_space_weight=1,\n",
    ")\n",
    "\n",
    "sl.PandasConverter.to_pandas(like_based_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aad1dcd6-55ea-4031-90c8-3ceea8797783",
   "metadata": {},
   "source": [
    "This feature enables reusing the query for different setting by simply supplying it with alternate weightings."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "superlinked-py3.11",
   "language": "python",
   "name": "superlinked-py3.11"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
